<?php

/**
 * Persistance strategy based on session.
 *
 * This strategy is well for middle length forms (3-6 pages)
 *
 * @author Piotr Śliwa <peter.pl7@gmail.com>
 */
class psPageableFormSessionPersistanceStrategy implements psPageableFormPersistanceStrategy
{
  private 
    $user = null,
    $name;

  /**
   * @param sfUser $user User object
   * @param string $name Name of user's attribute contains form values
   */
  public function __construct(sfUser $user, $name = 'PageableForm')
  {
    $this->user = $user;
    $this->name = (string) $name;
  }

  public function persist(psPageableForm $form)
  {
    $values = ((array) $form->getValues()) + $this->getValues();

    $this->user->setAttribute($this->name, $values);
  }

  public function getValues()
  {
    return (array) $this->user->getAttribute($this->name);
  }

  public function clear()
  {
    $this->user->setAttribute($this->name, null);
  }
}